<?php
/**
 * Модель оргструктуры
 */
class OrganizationModel extends AbstractModel{
  protected $tableName = 'organization';
  protected $primaryKey = array('organization_id');
  protected $columnTypes = array('organization_id'=>'uuid');
  public  $data = array(
      'organization_id'=>'',
      'parent_id'=>NULL,
      'head_user_id'=>NULL,
      'company_id'=>'',
      'name'=>'',
      'description'=>'',
  );


	static public function getByID($id){
		$db = DBFacade::getConnection();
		$sql='
			SELECT * FROM organization
      WHERE organization_id = ?
		';
    $dbh = $db->prepare($sql);
		$dbh->execute(array($id));
    $line = $dbh->fetch();
		if (!empty($line)) return new OrganizationModel($line);
		else return false;
	}

  /**
   * Достать все элементы оргструктуры по ID компании
   * Возвращает в ассоциированном массиве с ключами равными ID записи
   * @param String $id - company_id
   * @return array of OrganizationModel
   */
  static public function getByCompanyIdAsDictionary($id){
		$db = DBFacade::getConnection();
		$sql='
			SELECT * FROM organization
      WHERE company_id = ?
		';
    $dbh = $db->prepare($sql);
		$dbh->execute(array($id));
    $lines = $dbh->fetchAll();
		$result = array();
    foreach ($lines as $line){
      $result[$line['organization_id']] = new OrganizationModel($line);
    }
		return $result;
	}

}


